class Solution {
public:
    int largestRectangleArea(vector<int>& heights) {
        int area = 0;
        if(heights.size() == 0) return area;
        heights.insert(heights.begin() , 0);
        heights.insert(heights.end() , 0);
        stack<int> st;
        st.push(0);
        for(int i = 1;i < heights.size();i++){
            while(!st.empty() && heights[i] < heights[st.top()]){
                int x = st.top();
                st.pop();
                area = max(area, (i - st.top() - 1) * heights[x]);
            }
            st.push(i);
        }
        return area;
    }
};
